package com.youlu.campus.service.visitor.impl;

import com.youlu.campus.entity.UserAppSubWechatTemplateMsg;
import com.youlu.campus.entity.UserAppSubWechatTemplateMsgLog;
import com.youlu.campus.service.visitor.UserAppSubWechatTemplateMsgLogService;
import com.youlu.campus.service.visitor.UserAppSubWechatTemplateMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class UserAppSubWechatTemplateMsgServiceImpl implements UserAppSubWechatTemplateMsgService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserAppSubWechatTemplateMsgLogService userAppSubWechatTemplateMsgLogService;

    @Override
    public boolean create(UserAppSubWechatTemplateMsg req) {
        UserAppSubWechatTemplateMsg userAppSubWechatTemplateMsg = this.find(req.getUserId(), req.getPlatform(), req.getAppId(), req.getTemplateId());
        boolean isCreated = false;
        if(Objects.isNull(userAppSubWechatTemplateMsg)){
            req.setId(null);
            req.setCreatedTime(new Date());
            mongoTemplate.insert(req);
            isCreated = true;
        }else {
            if(userAppSubWechatTemplateMsg.getDeleted()) {
                Query query = new Query().addCriteria(Criteria.where("id").is(userAppSubWechatTemplateMsg.getId()));
                Update update = new Update();
                update.set("deleted", Boolean.FALSE);
                update.set("subTime", req.getSubTime());
                mongoTemplate.updateFirst(query, update, UserAppSubWechatTemplateMsg.class);
                isCreated = true;
            }
        }
        //重置失效模板id状态
        Query queryAllByUserId = new Query().addCriteria(Criteria.where("userId").is(req.getUserId()).and("platform").is(req.getPlatform()).and("appId").is(req.getAppId()).and("deleted").is(Boolean.TRUE));
        long count = mongoTemplate.count(queryAllByUserId, UserAppSubWechatTemplateMsg.class);
        if(count>0) {
            Update updateAllByUserId = new Update();
            updateAllByUserId.set("deleted", Boolean.FALSE);
            mongoTemplate.updateMulti(queryAllByUserId, updateAllByUserId, UserAppSubWechatTemplateMsg.class);
        }

        if(isCreated) {
            UserAppSubWechatTemplateMsgLog userAppSubWechatTemplateMsgLog = new UserAppSubWechatTemplateMsgLog();
            BeanUtils.copyProperties(req, userAppSubWechatTemplateMsgLog);
            userAppSubWechatTemplateMsgLogService.create(userAppSubWechatTemplateMsgLog);
        }
        return isCreated;
    }

    @Override
    public UserAppSubWechatTemplateMsg find(String userId, String platform, String appId, String templateId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("platform").is(platform).and("appId").is(appId).and("templateId").is(templateId));
        return mongoTemplate.findOne(query, UserAppSubWechatTemplateMsg.class);
    }

    @Override
    public boolean exist(String userId, String platform, String appId, String templateId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("platform").is(platform).and("appId").is(appId).and("templateId").is(templateId));
        return mongoTemplate.exists(query, UserAppSubWechatTemplateMsg.class);
    }

    @Override
    public boolean subscribed(String userId, String platform, String appId) {
        Query subQuery = new Query().addCriteria(Criteria.where("userId").is(userId).and("platform").is(platform).and("appId").is(appId));
        long subCount = mongoTemplate.count(subQuery, UserAppSubWechatTemplateMsg.class);
        Query sendQuery = new Query().addCriteria(Criteria.where("userId").is(userId).and("platform").is(platform).and("appId").is(appId).and("deleted").is(Boolean.TRUE));
        long sendCount = mongoTemplate.count(sendQuery, UserAppSubWechatTemplateMsg.class);
        return  subCount>0 && sendCount<=0;
    }
}
